<template>
  <el-form
    ref="form"
    :label-position="device === 'mobile' ? 'top' : 'right'"
    label-width="100px"
    :model="form"
    :rules="rules"
  >
    <vab-variety @selectData="selectData" />
    <el-form-item label="批次号" prop="batchCode">
      <el-input
        v-model.trim="form.batchCode"
        autocomplete="off"
        :disabled="true"
        placeholder="请输入批次号"
      />
    </el-form-item>
    <el-form-item label="注射时间" prop="injectionTime">
      <el-input
        v-model.number="form.injectionTime"
        autocomplete="off"
        placeholder="请输入注射时间"
      >
        <template slot="append">天</template>
      </el-input>
    </el-form-item>
    <el-form-item label="疫苗名称" prop="name">
      <el-input
        v-model.trim="form.name"
        autocomplete="off"
        placeholder="请输入疫苗名称"
      />
    </el-form-item>
    <el-form-item v-if="device === 'mobile'" class="submit-content">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from "vuex";
import { API_vaccinePlanCreate, API_vaccinePlanEdit } from "@/api/vaccinePlan";
import VabVariety from "@/components/VabVariety";
export default {
  name: "FormEdit",
  components: { VabVariety },
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        batchCode: "",
        injectionTime: "",
        name: "",
      },
      rules: {
        batchCode: [{ required: true, trigger: "blur", message: "请输入批次号" }],
        injectionTime: [
          { required: true, trigger: "blur", message: "请输入注射时间" },
        ],
        name: [{ required: true, trigger: "blur", message: "请输入饲料名称" }],
      },
    };
  },
  computed: {
    ...mapGetters({
      device: "settings/device",
    }),
  },
  watch: {
    formData: {
      handler(newData) {
        if (newData) {
          this.form = Object.assign({}, this.formData);
        }
      },
      immediate: true,
    },
  },
  methods: {
    selectData(data) {
      this.form.batchCode = data.batchCode;
      this.$forceUpdate();
      this.$refs["form"].clearValidate();
    },
    close() {
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
      if (this.device === "mobile") {
        return this.$router.push({
          name: "VaccinePlan",
          params: { refresh: true },
        });
      }
    },
    save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (this.form.code) {
            const { message } = await API_vaccinePlanEdit(this.form);
            this.$baseMessage(message, "success");
          } else {
            const { message } = await API_vaccinePlanCreate(this.form);
            this.$baseMessage(message, "success");
          }
          this.$emit("fetch-data");
          this.close();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
